﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Text;
using System.Net;

using LRSTaxServices.Biz;

public partial class Services : System.Web.UI.Page
{
    #region Public Properties

    protected TaxServicesSchedular schedularobj;
    public TaxDocumentsCollection docobj;
    public UserTaxDocuments userdoc;
    public string scheduledate;

    #endregion

    #region Event Handlers

    #region Page Load

    protected void Page_Load(object sender, EventArgs evt)
    {
        if (!Page.IsPostBack)
        {
            //InitMaxFieldLengths();
        }
    }

    #endregion

    #region Schedular Click Event

    protected void btnsubmit_Click(object sender, EventArgs e)
    {
        schedularobj = TaxServicesSchedular.CreateNew();
        schedularobj.FullName = txtname.Text.Trim();
        if (Request["txtjobstart"] != null)
        {
            scheduledate = Request["txtjobstart"].ToString();
            schedularobj.ScheduleDate = scheduledate;
        }
        schedularobj.ScheduleTime = txttime.Text.Trim();
        schedularobj.Phone = txtphone.Text.Trim();
        TaxServicesSchedular.Update(schedularobj);

        string emailfeedback;
        bool emailsend = LRSTaxServices.MailUtils.SendEmail("sachin.verma@vermasoftech.com", "admin", "New Schedule Request", "A new Schedule has been requested by Mr./Mrs. " + txtname.Text + " with the following details :-<br><br> Name : " + txtname.Text + " <br> Schedule Date : " + scheduledate + " <br> Schedule Time : " + txttime.Text + " <br> Phone : " + txtphone.Text + " <br><br> This is an auto generated mail,please do not reply.", out emailfeedback);
        txtname.Text = string.Empty;
        txtphone.Text = string.Empty;
        txttime.Text = string.Empty;
    }

    #endregion

    #region Personal Tax Form Upload Click Event

    protected void btnpersonal_Click(object sender, EventArgs e)
    {
        if (uploadpersonaltaxforms.HasFile)
        {
            string filename = "";
            filename = uploadpersonaltaxforms.FileName;
            string fileextension = Path.GetExtension(filename);
            string filewithoutext = Path.GetFileNameWithoutExtension(filename);
            string uniquekeyname = filewithoutext + Guid.NewGuid().ToString() + fileextension;
            if (fileextension.Equals(".doc") || fileextension.Equals(".docx") || fileextension.Equals(".xls") || fileextension.Equals(".xlsx") || fileextension.Equals(".pdf"))
            {
                uploadpersonaltaxforms.SaveAs(Server.MapPath("UserTaxDocuments" + "\\" + uniquekeyname));
                userdoc = UserTaxDocuments.CreateNew();
                userdoc.DocumentType = "Personal Tax Preparation";
                userdoc.UploadedFileName = uniquekeyname;
                UserTaxDocuments.Update(userdoc);
                lblerrorp.Visible = false;

                //Generate the form to post to PayPal    
                Response.Write(PayPalPostForm());
                //Post the form to PayPal    
                this.PayPalPostScript(Page);
                //Response.Redirect("TaxFeesSuccess.aspx?amt=200&tx=12456KLHN78");
            }
            else
            {
                lblerrorp.Visible = true;
                lblerrorp.Text = "you can upload only .doc,.docx,.xls,.xlsx and.pdf formet supported files.";
            }
        }
        else
        {
            lblerrorp.Visible = true;
            lblerrorp.Text = "*please select file to upload";
        }
    }

    #endregion

    #region Bussines Tax Form Upload Click Event

    protected void btnbsines_Click(object sender, EventArgs e)
    {
        if (uploadbusinestaxforms.HasFile)
        {
            string filename = "";
            filename = uploadbusinestaxforms.FileName;
            string fileextension = Path.GetExtension(filename);
            string filewithoutext = Path.GetFileNameWithoutExtension(filename);
            string uniquekeyname = filewithoutext + Guid.NewGuid().ToString() + fileextension;
            if (fileextension.Equals(".doc") || fileextension.Equals(".docx") || fileextension.Equals(".xls") || fileextension.Equals(".xlsx") || fileextension.Equals(".pdf"))
            {
                uploadbusinestaxforms.SaveAs(Server.MapPath("UserTaxDocuments" + "\\" + uniquekeyname));
                userdoc = UserTaxDocuments.CreateNew();
                userdoc.DocumentType = "Business Tax Preparation";
                userdoc.UploadedFileName = uniquekeyname;
                UserTaxDocuments.Update(userdoc);
                lblerrorb.Visible = false;

                //Generate the form to post to PayPal    
                Response.Write(PayPalPostForm());
                //Post the form to PayPal    
                this.PayPalPostScript(Page);
            }
            else
            {
                lblerrorb.Visible = true;
                lblerrorb.Text = "you can upload only .doc,.docx,.xls,.xlsx and.pdf formet supported files.";
            }
        }
        else
        {
            lblerrorb.Visible = true;
            lblerrorb.Text = "*please select file to upload";
        }
    }

    #endregion

    #endregion

    #region Private Methods

    public string PayPalPostForm()
    {
        //PayPal cart version    
        string PostUrl = PayPalConstants.WEBSCR_URL;
        string Cmd = "_cart";
        string Upload = "1";
        string BusinessEmail = PayPalConstants.STANDARDBUSINESS_EMAIL_ADDRESS;
        string Currency = "USD";
        string Method = "post";
        string imageurl = "http://localhost:1612/LRSTaxServices.Site/images/logo.png";
        string returnurl = "http://localhost:1612/LRSTaxServices.Site/Services.aspx";
        string cancelreturnurl = "http://localhost:1612/LRSTaxServices.Site/Default.aspx";
        //string ShipAmount = "0";

        //Create the Form to write to the page with PayPal parameters    
        StringBuilder ppForm = new StringBuilder();
        ppForm.AppendFormat("<Form name='frmPP' id='frmPP' action='{0}' method='{1}'>", PostUrl, Method);
        //ppForm.AppendFormat("<input type='hidden' name='shipping' value='{0}'>", ShipAmount);
        ppForm.AppendFormat("<input type='hidden' name='cmd' value='{0}'>", Cmd);
        ppForm.AppendFormat("<input type='hidden' name='upload' value='{0}'>", Upload);
        ppForm.AppendFormat("<input type='hidden' name='image_url' value='{0}'>", imageurl);
        ppForm.AppendFormat("<input type='hidden' name='return' value='{0}'>", returnurl);
        ppForm.AppendFormat("<input type='hidden' name='cancel_ return' value='{0}'>", cancelreturnurl);
        ppForm.AppendFormat("<input type='hidden' name='business' value='{0}'>", BusinessEmail);
        ppForm.AppendFormat("<input type='hidden' name='currency_code' value='{0}'>", Currency);
        //ppForm.AppendFormat("<input type='hidden' name='item_number_1' value='{0}'>", 1);
        ppForm.AppendFormat("<input type='hidden' name='item_name_1' value='{0}'>", "LRS Tax fees");
        ppForm.AppendFormat("<input type='hidden' name='amount_1' value='{0}'>", "200");
        //ppForm.AppendFormat("<input type='hidden' name='quantity_1' value='{0}'>", 3);
        ppForm.Append("</form>");
        return ppForm.ToString();
    }

    private void PayPalPostScript(System.Web.UI.Page Page)
    {
        //This registers Javascript to the page which is used to post the PayPal Form details    
        StringBuilder strScript = new StringBuilder();
        strScript.Append("<script language='javascript'>");
        strScript.Append("var ctlForm = document.getElementById('frmPP');");
        strScript.Append("ctlForm.submit();");
        strScript.Append("</script>");
        ClientScript.RegisterClientScriptBlock(this.GetType(), "PPSubmit", strScript.ToString());
    }

    #endregion   
}
